﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Threading;
using Intorise.Common;
using MuAD;

namespace MUClient
{
    public class AdPlayer : Player
    {
        public AdPlayer(Account account, BotConfig config):base(account,config)
        {

        }

        public override void InitProcessor()
        {
            engine.RegisterProcessor(new Processor("ProcessServerResult", MessageTypeEnum.ServerResult, ProcessServerResult));
            engine.RegisterProcessor(new Processor("ProcessMapEntered", MessageTypeEnum.MapEntered, ProcessMapEntered));
        }

        /// <summary>
        /// 执行脚本前准备(获取角色信息等)
        /// </summary>
        /// <returns>是否已准备好</returns>
        public override bool InitUserData()
        {
            #region 换线
            if (Config.RandomLine)
            {
                Change(new Random(new Random((int)DateTime.Now.Ticks).Next()).Next(10) + 1);
            }
            else if (this.CurrentLine > 0)
            {
                Change(this.CurrentLine);
            }
            else
            {
                Change(Config.LineNumber);
            }
            Thread.Sleep(1000);
            #endregion

            Zuidui();//组队
            Thread.Sleep(1000);
            Tuizu();//退组

            DateTime begin = DateTime.Now;
            while (Level <= 0)
            {
                if (LogDebugInfo)
                {
                    ConsoleBLL.WriteLine(this + " 等待获取当前等级");
                }
                var seconds = (DateTime.Now - begin).TotalSeconds;
                if (seconds > 60)
                {
                    ConsoleBLL.WriteLineError(this + " 获取当前等级超时，退出..."); return false;
                }
                if (seconds > 30)
                {
                    EnterMap();
                    Zuidui();//组队
                    Thread.Sleep(300);
                    Tuizu();//退组
                }
                Thread.Sleep(1000);
            }
            return true;
        }

        public override void Play()
        {
            if (!InitUserData())
            {
                ConsoleBLL.WriteLineError(this + "初始化游戏失败！"); Quit(PlayerStatusEnum.等待); return;
            }
            if (this.Level < CompletedLevel)
            {
                ConsoleBLL.WriteLineError(this + "等级不到" + CompletedLevel + "，不能喊话！");
                Quit(PlayerStatusEnum.停止); return;
            }
            Speak();
            Quit(PlayerStatusEnum.任务完成);
        }

        public void Speak()
        {
            try
            {
                var data = SpeakHelper.GetChatData();
                if (data.Length == 0)
                {
                    ConsoleBLL.WriteLineError("聊天数据为空！");//手饰 元素 铂晶 旺汪：cymyl +扣 4102 48569 
                    data = new byte[] { 0x00, 0x00, 0x00, 0x3F, 0x00, 0x01, 0xB2, 0x61, 0xAE, 0x1A, 0x8C, 0x93, 0xA1, 0x2E, 0xC0, 0xFD, 0x47, 0x66, 0xC8, 0xAC, 0xBB, 0x69, 0x72, 0x32, 0xA3, 0x8F, 0xE2, 0x3E, 0xED, 0x7C, 0x72, 0x89, 0x69, 0xF5, 0x89, 0x3B, 0x0C, 0xEE, 0x3F, 0xB7, 0x79, 0x31, 0x4C, 0x7D, 0x32, 0xB0, 0x7F, 0x99, 0x67, 0xD0, 0x6D, 0x8A, 0x63, 0xDE, 0x6C, 0x68, 0xBE, 0xC1, 0xEF, 0x73, 0x54, 0x03, 0xC0, 0x43, 0xF4, 0x10, 0x34 };
                }
                engine.SendData(data);
                Thread.Sleep(1000);
            }
            catch (Exception ex)
            {
                ConsoleBLL.WriteLineError("加载聊天数据失败！\r\n" + ex.Message);
            }
        }
    }
}
